package com.niit.happynicedays;

import java.io.FileInputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class halloween extends Activity implements OnTouchListener {

	ImageView imBackground, imBat, imMummy, imPumpkin;
	MediaPlayer mMedia;
	Handler batHandler, mummyHandler;
	Thread batThread, mummyThread;
	int SCREENWIDTH, SCREENHEIGHT;
	int xBat = 0, xmummy = 0;
	Boolean batFlag = false;
	Boolean mummyFlag = false;
	Boolean flag = true;
	DisplayMetrics display;
	float volume;
	String filename = "config.cfg";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.halloween);

		try {
			FileInputStream fis = openFileInput(filename);
			volume = (float) fis.read() / 10;
			fis.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

		display = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(display);
		SCREENWIDTH = display.widthPixels;
		SCREENHEIGHT = display.heightPixels;

		imBackground = (ImageView) findViewById(R.id.halloween_bg);
		imBackground.setBackgroundResource(R.drawable.halloween_bg);

		mMedia = MediaPlayer.create(getBaseContext(), R.raw.soihu);
		mMedia.setVolume(volume, volume);
		mMedia.setLooping(true);
		mMedia.start();

		imBat = (ImageView) findViewById(R.id.bat);
		imBat.setBackgroundResource(R.drawable.bat_1);
		setLayout(imBat, 0, 0, -200, -300);
		imBat.setOnTouchListener(this);

		imMummy = (ImageView) findViewById(R.id.mummy);
		imMummy.setBackgroundResource(R.drawable.mummy_1);
		setLayout(imMummy, SCREENWIDTH - DPtoPX(120), SCREENHEIGHT
				- DPtoPX(130), -100, -100);
		imMummy.setOnTouchListener(this);

		imPumpkin = (ImageView) findViewById(R.id.pumpkin);
		imPumpkin.setBackgroundResource(R.drawable.pumpkin_1);
		setLayout(imPumpkin, 0, (4 * SCREENHEIGHT / 5) - 100, -200, -200);
		imPumpkin.setOnTouchListener(this);

		batHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub

				if (xBat <= 26) {
					imBat.setBackgroundResource(R.anim.bat);
					AnimationDrawable batAnim = (AnimationDrawable) imBat
							.getBackground();
					if (batFlag) {
						batAnim.start();
					} else {
						batAnim.stop();
					}
					setLayout(imBat, tinhx(xBat), 0, -200, -300);
				} else {
					imBat.setBackgroundResource(R.anim.bat_1);
					AnimationDrawable batAnim = (AnimationDrawable) imBat
							.getBackground();
					if (batFlag) {
						batAnim.start();
					} else {
						batAnim.stop();
					}
					setLayout(imBat, tinhx(xBat), 0, -200, -300);
				}
				super.handleMessage(msg);
			}
		};

		mummyHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub

				if (xmummy <= 26) {
					imMummy.setBackgroundResource(R.anim.mummy);
					AnimationDrawable mummyAnim = (AnimationDrawable) imMummy
							.getBackground();
					if (mummyFlag) {
						mummyAnim.start();
					} else {
						mummyAnim.stop();
					}
					setLayout(imMummy, SCREENWIDTH - DPtoPX(120)
							- tinhx(xmummy), SCREENHEIGHT - DPtoPX(130), -200,
							-300);
				} else {
					imMummy.setBackgroundResource(R.anim.mummy1);
					AnimationDrawable mummyAnim = (AnimationDrawable) imMummy
							.getBackground();
					if (mummyFlag) {
						mummyAnim.start();
					} else {
						mummyAnim.stop();
					}
					setLayout(imMummy, SCREENWIDTH - DPtoPX(120)
							- tinhx(xmummy), SCREENHEIGHT - DPtoPX(130), -200,
							-300);
				}
				super.handleMessage(msg);
			}
		};

	}

	private void animBat() {
		if (batFlag) {
			batFlag = false;
		} else {
			batFlag = true;
		}
		batThread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (batFlag) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					batHandler.sendEmptyMessage(0);
					xBat += 1;
					if (xBat == 53) {
						xBat = 0;
					}
				}
			}
		});
		batThread.start();
	}

	private void animMummy() {
		if (mummyFlag) {
			mummyFlag = false;
		} else {
			mummyFlag = true;
		}
		mummyThread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (mummyFlag) {
					try {
						Thread.sleep(150);
					} catch (InterruptedException e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					mummyHandler.sendEmptyMessage(0);
					xmummy += 1;
					if (xmummy == 53) {
						xmummy = 0;
					}
				}
			}
		});
		mummyThread.start();
	}

	private void animPumpkin() {
		if (imPumpkin != null) {
			imPumpkin.setBackgroundResource(R.anim.pumpkin);

			AnimationDrawable ani = (AnimationDrawable) imPumpkin
					.getBackground();
			if (ani.isRunning()) {
				ani.stop();
			} else {
				ani.stop();
				ani.start();
			}
		}
	}

	private void setLayout(View v, int left, int top, int right, int bottom) {
		RelativeLayout.LayoutParams privew = new RelativeLayout.LayoutParams(
				Math.round(v.getLayoutParams().width), Math.round(v
						.getLayoutParams().height));

		privew.setMargins(left, top, right, bottom);
		v.setLayoutParams(privew);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(getBaseContext(), nicedays.class);
			startActivity(intent);
			mMedia.stop();
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bat: {
			animBat();
			break;
		}
		case R.id.mummy: {
			animMummy();
			break;
		}
		case R.id.pumpkin: {
			animPumpkin();
			break;
		}
		default:
			break;
		}
		return false;
	}

	public int tinhx(int x) {
		if (x <= 26) {
			return (8 * x);
		} else {
			return (-8 * x + 416);
		}
	}

	public int DPtoPX(int x) {
		return ((int) (display.density * x));
	}
}
